import { FindOneOptions } from 'typeorm'
import { EntityMixin } from '../mixins'
import User from './User'

// 携带了存储库实例的服务类
export default class UserService extends EntityMixin(User) {
  // 根据用户ID获取用户信息
  static async getUserInfoByUserID(userId: number, select?: FindOneOptions<User>['select']) {
    if (!userId) {
      return null
    }
    return await UserService.getRepository().findOne({ select, where: { id: userId } })
  }

  // 根据用户名获取用户信息
  static async getUserInfoByUsername(username: string, select?: FindOneOptions<User>['select']) {
    if (!username) {
      return null
    }
    return await UserService.getRepository().findOne({ select, where: { username } })
  }
}
